﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Security.Application;
using ThreeDPrinting.ORM;
using ThreeDPrinting.Utilities;

namespace ThreeDPrinting.Pages.LogIn
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            {
                if (Session["memberID"] != null)
                    Response.Redirect("~/");
            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            using (DBEntities entities = new DBEntities())
            {
                tblmember mem = new tblmember();
                string cleanUserName = Sanitizer.GetSafeHtmlFragment(txtLoginUserName.Text);
                string decodeUserName = HttpUtility.HtmlDecode(cleanUserName);
                string trimmedUserName = (cleanUserName == null) ? null : cleanUserName.Trim();

                string cleanPassword = Sanitizer.GetSafeHtmlFragment(txtLoginPassword.Text);
                string decodePassword = HttpUtility.HtmlDecode(cleanPassword);
                string trimmedPassword = (cleanPassword == null) ? null : cleanPassword.Trim();
                
                mem = entities.tblmembers.SingleOrDefault(p => p.memberPassword == trimmedPassword  && p.memberUserName == trimmedUserName);

                if (mem != null)
                {
                    Session["memberID"] = mem.memberID;
                    Response.Redirect("~/");
                }
            }
        }

        protected void btnForgotPassword_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/LogIn/ForgotPassword.aspx");
        }
    }
}